package com.then.architecture.databinding;

import android.databinding.BindingAdapter;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;


/**
 * Created by asus on 2017/6/9.
 */

public class ImageViewAdapter {

    @BindingAdapter(value = {"url", "resId", "isCircle","isLocal"}, requireAll = false)
    public static void image(ImageView imageView, String url, int resId, boolean isCircle,boolean isLocal) {
        if (!TextUtils.isEmpty(url) && !url.trim().isEmpty()) {
            RequestOptions options;
            if (isCircle) {
                options = RequestOptions.circleCropTransform();
            } else {
                options = new RequestOptions();
            }
            options = options.format(DecodeFormat.PREFER_RGB_565).error(resId);
            if (isLocal) {  //本地图片
                Glide.with(imageView.getContext()).load(new File(url)).apply(options).
                        into(imageView);
            } else {
                Glide.with(imageView.getContext()).load(url).apply(options).
                        into(imageView);
            }
        } else {
            if (resId != 0) {
                imageView.setImageResource(resId);
            }
        }
    }

}
